<?php

namespace Home\Model;
use Think\Model;
class ReceiveLabelModel extends BaseModel{
    
    //站点标签信息表信息表
    protected $tableName = 'tp_receive_label';

    /**
     * @Desc  : 查询站点下标签
     * @Author：quyang
     * @Param ：$receiveId 收件人id
     * @Time  ：2017/3/22 10:43
     * @Update：2017/3/22
     **/
    public function queryLabel( $receiveId , $type = '' )
    {

        $receiveId = $this->dataArray( $receiveId );

        $where['rl.f_receive_id'] = array( 'in' , $receiveId );
        $where['rl.f_type'] = 0;

        if ( $type != '' )
        {
            $where['l.f_type'] = $type;
        }
        else
        {
            $where['l.f_type'] = ['in',[1,2]];
        }

        return $this->alias('rl')
             ->where( $where )
             ->field('
                rl.f_receive_id,l.f_id , l.f_label,l.f_send_type
             ')
             ->join( 'left join tp_label l ON rl.f_receive_label_id = l.f_id' )
             ->select();

    }

    /**
     * @Desc  : 数组校验
     * @Author：quyang
     * @Param ：
     * @Time  ：2017/3/23 10:23
     * @Update：2017/3/23
     **/
    public function dataArray( $data )
    {

        if ( is_array( $data ) )
        {
            return $data;
        }
        else
        {
            return array( $data );
        }

    }

    /**
     * @Desc  : 根据标签查找联系人信息
     * @Author：quyang
     * @Param ：$stationId 站点id  $larbelId 标签id
     * @Time  ：2017/3/22 14:48
     * @Update：2017/3/22
     **/
    public function stationRecipient( $stationId ,$larbelId = ''  )
    {

        $where['rl.f_station_id'] = $stationId;
        if ( $larbelId != '' )
        {
            $where['rl.f_receive_label_id'] = $larbelId;
        }

        return $this->alias('rl')
            ->where( $where )
            ->field("
                m.f_id as fId,
                m.f_recipient_mobile as fRecipientMobile,
                m.f_recipient_name as fRecipientName,
                m.f_recipient_phone as fRecipientPhone,
                m.f_address as fAddress,
                m.f_status as fStatus
            ")
            ->join( 'left join tp_mobile_info m on rl.f_receive_id = m.f_id' )
            ->select();

    }

    /**
     * @Desc  : 根据标签查找联系人信息
     * @Author：quyang
     * @Param ：$stationId 站点id  $larbelId 标签id
     * @Time  ：2017/3/22 14:48
     * @Update：2017/3/22
     **/
    public function countStationRecipient( $stationId ,$larbelId = ''  )
    {

        $where['rl.f_station_id'] = $stationId;
        if ( $larbelId != '' )
        {
            $where['rl.f_receive_label_id'] = $larbelId;
        }

        return $this->alias('rl')
            ->where( $where )
            ->join( 'left join tp_mobile_info m on rl.f_receive_id = m.f_id' )
            ->count('m.f_id');

    }

    /**
     * @Desc  : 查询站点标签是否存在
     * @Author：quyang
     * @Param ：$stationId 站点id  $larbelId 标签id
     * @Time  ：2017/3/22 11:20
     * @Update：2017/3/22
     **/
    public function queryLabelExistence( $labelId , $stationId , $recipientId )
    {

        $where['f_receive_label_id'] = $labelId;
        $where['f_station_id'] = $stationId;
        $where['f_receive_id'] = $recipientId;

        return $this->where( $where )->getField( 'f_id' );

    }

    /**
     * @Desc  : 修改标签状态
     * @Author：quyang
     * @Param ：$stationId 站点id  $larbelId 标签id  $recipientId收件人id  $type 修改状态（0.正常 -1.删除）
     * @Time  ：2017/3/22 13:12
     * @Update：2017/3/22
     **/
    public function updateLabelLog( $labelId , $stationId , $recipientId , $type )
    {

        $where['f_receive_label_id'] = $labelId;
        $where['f_station_id']       = $stationId;
        $where['f_receive_id']       = $recipientId;
        $map['f_type']               = intval( $type );

        return $this->where( $where )->save( $map );

    }

    /**
     * @Desc  : 站点添加新的标签
     * @Author：quyang
     * @Param ：$stationId 站点id  $larbelId 标签id  $recipient 联系人ID
     * @Time  ：2017/3/22 13:15
     * @Update：2017/3/22
     **/
    public function addLabelLog( $labelId , $stationId , $recipient )
    {

        $map = array(
            'f_station_id'          =>  $stationId,
            'f_receive_label_id'    =>  $labelId,
            'f_receive_id'          =>  $recipient
        );

        return $this->add( $map );
    }

    /**
     * @Desc  : 查询收件人标签数量
     * @Author：quyang
     * @Param ：
     * @Time  ：2017/3/28 10:47
     * @Update：2017/3/28
     **/
    public function countReceiveLabel( $staionId , $receiveId )
    {

        $where['f_station_id'] = $staionId;
        $where['f_receive_id'] = $receiveId;
        $where['f_type'] = 0;

        return $this->where( $where )->count('f_id');

    }

    /**
     * @Desc  : 查询收件人所有标签
     * @Author：quyang
     * @Param ：
     * @Time  ：2017/4/26 14:36
     * @Update：2017/4/26
     **/
    public function querySendList( $stationId , $receiveId )
    {

        $where = "`f_station_id` = {$stationId} AND `f_receive_id` = {$receiveId}";

        return $this->where( $where )->field( 'f_id,f_receive_label_id' )->select();

    }

    /**
     * @Desc  : 修改标签（只针对发送信息标签）
     * @Author：quyang
     * @Param ：$fId 列表id  $larbelId 标签id
     * @Time  ：2017/4/26 14:22
     * @Update：2017/4/26
     **/
    public function updateSendLabel( $fId , $labelId )
    {

        $where = "`f_id` = {$fId}";

        $map['f_receive_label_id'] = $labelId;

        return $this->where( $where )->save( $map );

    }

    /**
     * @Desc  : 查询收件人是否需要发送通知
     * @Author：quyang
     * @Param ：
     * @Time  ：2017/4/26 16:25
     * @Update：2017/4/26
     **/
    public function querySendLabelStatus( $stationId , $mobile )
    {

        $where = "
            `f_station_id` = {$stationId}
            AND `f_receive_id` = (
                SELECT
                    `f_id`
                FROM
                    tp_mobile_info
                WHERE
                    `f_user_id` = {$stationId}
                AND `f_recipient_mobile` = {$mobile}
                LIMIT 1
            )
            AND `f_receive_label_id` IN (
                SELECT
                    `f_id`
                FROM
                    tp_label
                WHERE
                    `f_type` = 3
            )
        ";

        return $this->where( $where )->find();

    }


}